package com.sicau.controller;

import com.sicau.constants.CommonConstants;
import com.sicau.entity.dto.*;
import com.sicau.entity.pojo.po.ComposerPO;
import com.sicau.entity.pojo.vo.ResultVO;
import com.sicau.service.ManagerService;
import com.sicau.utils.JWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 管理者controller
 * @author ttxxi
 */

@RestController
public class ManagerController {

    @Autowired
    ManagerService managerService;

    /**
     * 管理员登录
     * @param response 因为不进入拦截器，所以在此手动设置跨域
     * @param manager 用于接受id password
     * @return
     */
    @RequestMapping(CommonConstants.PUB_PREFIX+"/manager/login")
    public ResultVO login(HttpServletResponse response,Manager manager){
        response.addHeader("Access-Control-Allow-Methods","*");
        response.addHeader("Access-Control-Allow-Origin","*");
        response.addHeader("Access-Control-Allow-Headers", "Content-Type,token,Cookie");
        response.addHeader("Access-Control-Allow-Credentials","true");
        response.setHeader("token", JWT.sign(manager, 10800000));
        response.setHeader("Access-Control-Expose-Headers", "token");
        System.out.println(manager);
        return managerService.login(manager);
    }

    /**
     * 管理员获取所有任务
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/index")
    public ResultVO index(){
        return managerService.index();
    }

    /**
     *管理员进入用户信息界面
     * @param id 用户id
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/composerInfo")
    public ResultVO composerInfo(String id){
        return managerService.composerInfo(id);
    }

    /**
     * 管理员  创建  或  修改    新任务
     * @param taskInfo 用来接收任务信息的实体类
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/setTask")
    public ResultVO setTask(String option,TaskInfo taskInfo){
        return managerService.setTask(option,taskInfo);
    }

    /**
     * 根据id删除task
     * @param taskId taskid
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/deleteTask")
    public ResultVO deleteTask(String taskId){
        return managerService.deleteTask(taskId);
    }

    /**
     * 修改公告
     * @param notice
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/modifyNotice")
    public ResultVO modifyNotice(Notice notice){
        return managerService.modifyNotice(notice);
    }

    /**
     * 获取所有用户信息
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/getComposer")
    public ResultVO getComposer(){
        return managerService.getComposer();
    }

    /**
     * 根据用户id获取详细信息以及密码
     * @param id
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/composer")
    public ResultVO composer(String id){
        return managerService.composer(id);
    }

    /**
     * 新建或修改账号
     * @param option
     * @param composerPO
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/setAccount")
    public ResultVO setAccount(String option, ComposerPO composerPO){
        return managerService.setAccount(option, composerPO);
    }

    /**
     * 修改用户密码
     * @param composer
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/changeComposerPassword")
    public ResultVO changeComposerPassword(Composer composer){
        return managerService.changeComposerPassword(composer);
    }

    /**
     * 修改用户状态
     * @param composer
     * @return
     */
    @RequestMapping(CommonConstants.NONPUBLIC_PREFIX+"/manager/changeComposerStatus")
    public ResultVO changeComposerStatus(Composer composer){
        return managerService.changeComposerStatus(composer);
    }

}
